package com.ymttest.business.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.apache.http.client.ClientProtocolException;

import com.google.gson.JsonArray;
import com.ymatou.iapi.livebuyer.parameter.*;
import com.ymatou.iapi.livebuyer.service.*;
import com.ymatou.iapi.liveseller.parameter.AddProductsToLiveBean;
import com.ymatou.iapi.liveseller.parameter.CloseSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.LiveProductListBean;
import com.ymatou.iapi.liveseller.parameter.RemoveLiveProductBean;
import com.ymatou.iapi.liveseller.parameter.RemoveSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.ResumeLiveProductBean;
import com.ymatou.iapi.liveseller.parameter.SellerLiveOnlineBean;
import com.ymatou.iapi.liveseller.parameter.SetLiveProductTopBean;
import com.ymatou.iapi.liveseller.parameter.SortLiveProductBean;
import com.ymatou.iapi.liveseller.parameter.SuspendLiveProductBean;
import com.ymatou.iapi.liveseller.parameter.SysCloseLiveBean;
import com.ymatou.iapi.liveseller.service.AddProductsToLiveCall;
import com.ymatou.iapi.liveseller.service.CloseSellerLiveCall;
import com.ymatou.iapi.liveseller.service.CreateSellerLiveCall;
import com.ymatou.iapi.liveseller.service.LiveProductListCall;
import com.ymatou.iapi.liveseller.service.RemoveLiveProductCall;
import com.ymatou.iapi.liveseller.service.RemoveSellerLiveCall;
import com.ymatou.iapi.liveseller.service.ResumeLiveProductCall;
import com.ymatou.iapi.liveseller.service.SellerLiveOnlineCall;
import com.ymatou.iapi.liveseller.service.SetLiveProductTopCall;
import com.ymatou.iapi.liveseller.service.SortLiveProductCall;
import com.ymatou.iapi.liveseller.service.SuspendLiveProductCall;
import com.ymatou.iapi.liveseller.service.SysCloseLiveCall;
import com.ymatou.livevideo.parameter.Drm;
import com.ymatou.livevideo.parameter.LiveNotifyBean;
import com.ymatou.livevideo.parameter.MetaData;
import com.ymatou.livevideo.parameter.ProcedureStateChanged;
import com.ymatou.livevideo.parameter.ProcessTask;
import com.ymatou.livevideo.parameter.RecLiveNotifyBean;
import com.ymatou.livevideo.parameter.ReplayNotifyBean;
import com.ymatou.livevideo.parameter.StreamLiveNotifyBean;
import com.ymatou.livevideo.parameter.Trancode;
import com.ymatou.livevideo.parameter.TrancodeInPut;
import com.ymatou.livevideo.parameter.TrancodeOutPut;
import com.ymatou.livevideo.service.LiveNotifyCall;
import com.ymt.base.BaseBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/************************************************************************************
 * @File name :LiveRoomCallService.java
 * @Author : zhouyi
 * @Date : 2017年10月30日
 * @Copyright : 洋码头
 ************************************************************************************/
/************************************************************************************
 * @File name :LiveRoomCallService.java
 * @Author : zhouyi
 * @Date : 2017年10月30日
 * @Copyright : 洋码头
 ************************************************************************************/
public class LiveRoomCallService {
	/**
	 * 直播推流key
	 */
	public static String LiveAntiKey = "7eac8d73ada557988fb1d0ffff89caed";

	/**
	 * 直播API鉴权key
	 */
	public static String LiveApiKey = "f21c3164a0c4e1e2a66748ca3b3c6b25";
	public static String AppId = "1255315550";
	public static String BizId = "12808";
	public static String ImSdkAppId = "1400046801";
	public static String ImAccountType = "18457";

	/**
	 * 发布直播状态 用于推送直播在线人数/比心数 定时调用
	 * 
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void PublishLiveStatCall(Integer liveid) throws InterruptedException, ClientProtocolException, IOException {
		Logger.comment("推送直播状态");
		Thread.sleep(1500);
		PublishLiveStatBean PublishLiveStatBean = new PublishLiveStatBean();
		PublishLiveStatCall PublishLiveStatCall = new PublishLiveStatCall();
		if(liveid!=null){
			PublishLiveStatBean.setLiveId(liveid);
			PublishLiveStatCall.setData(false, PublishLiveStatBean);
		}
		PublishLiveStatCall.callService();
		Thread.sleep(3000);
	}

	/**
	 * 查询房间信息
	 * 
	 * @param id
	 * @return
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static LiveRoomInfo getRoomInfo(int id) throws InterruptedException, ClientProtocolException, IOException {
		Logger.comment("查询房间信息");
		QueryLiveRoomBean queryliveroomBean = new QueryLiveRoomBean();
		QueryLiveRoomCall queryliveroomCall = new QueryLiveRoomCall();
		queryliveroomBean.setLiveId(id);
		queryliveroomCall.setData(false, queryliveroomBean);
		queryliveroomCall.callService();
		return queryliveroomCall.getLiveRoomInfo();
	}

	/**
	 * 初始化tx回调退断流信息
	 * 
	 * @param liveid
	 * @param event_type
	 *            1推流 0断流
	 * @return
	 * @throws Exception
	 *             真实报文
	 *             {"app":"12808.livepush.myqcloud.com","appId":"1255315550","appname":"live","channel_id":"12808_SIT1_235","errcode":1,"errmsg":"recv
	 *             rtmp
	 *             deleteStream","event_type":0,"node":"100.67.177.19","requestId":"781eaafd69764f7fa9f97d08e5c612cb","sequence":"1418989456100673054","sign":"5bbc740031c41e44d62c48058e4de804","stream_id":"12808_SIT1_235","stream_param":"txSecret=97f7e30da2082d7eefcad89371c04181&txTime=59F08C74","t":"1509006840","update_time":0,"user_ip":"114.80.87.210"}
	 */
	public static StreamLiveNotifyBean initStreamLiveNotifyBean(Integer id, int event_type) throws Exception {
		String sequence = TestDataManager.getRandomNum(9);
		Long ltime = YMTDateUtil.getDate().getTime();
		String time = Long.toHexString(ltime);
		StreamLiveNotifyBean streamlivenotifyBean = new StreamLiveNotifyBean();
		streamlivenotifyBean.setChannel_id(BizId + "_" + EnvSetup.getEnv() + "_" + id);
		streamlivenotifyBean.setStream_id(BizId + "_" + EnvSetup.getEnv() + "_" + id);
		streamlivenotifyBean.setT(String.valueOf(YMTDateUtil.getDate().getTime()));
		streamlivenotifyBean.setEvent_type(event_type);
		streamlivenotifyBean.setSign(TestDataManager.getMD5(id.toString().getBytes()));
		streamlivenotifyBean.setApp(BizId + ".livepush.myqcloud.com");
		streamlivenotifyBean.setNode(TestDataManager.getRandomIp());
		streamlivenotifyBean.setSequence(sequence);
		streamlivenotifyBean.setSign(TestDataManager.getMD5((LiveApiKey + ltime.toString()).getBytes()));
		streamlivenotifyBean.setT(ltime.toString());
		streamlivenotifyBean.setStream_param("bizid=" + BizId + "&txSecret="
				+ TestDataManager.getMD5((LiveRoomCallService.LiveAntiKey + LiveRoomCallService.BizId + "_" + id + Long.toHexString(ltime)).getBytes()) + "&txTime=" + time);
		return streamlivenotifyBean;
	}

	/**
	 * 初始化录播回调报文
	 * 
	 * @param id
	 * @param event_type
	 * @return
	 * @throws Exception
	 */
	public static RecLiveNotifyBean initRecLiveNotifyBean(Integer id, int event_type) throws Exception {
		String sequence = TestDataManager.getRandomNum(9);
		Long ltime = YMTDateUtil.getDate().getTime();
		String time = Long.toHexString(ltime);
		RecLiveNotifyBean reclivenotifyBean = new RecLiveNotifyBean();
		reclivenotifyBean.setChannel_id(BizId + "_" + EnvSetup.getEnv() + "_" + id);
		reclivenotifyBean.setStream_id(BizId + "_" + EnvSetup.getEnv() + "_" + id);
		reclivenotifyBean.setT(String.valueOf(YMTDateUtil.getDate().getTime()));
		reclivenotifyBean.setEvent_type(event_type);
		reclivenotifyBean.setSign(TestDataManager.getMD5(id.toString().getBytes()));
		reclivenotifyBean.setSign(TestDataManager.getMD5((LiveApiKey + ltime.toString()).getBytes()));
		reclivenotifyBean.setT(ltime.toString());
		reclivenotifyBean.setStream_param("bizid=" + BizId + "&txSecret="
				+ TestDataManager.getMD5((LiveRoomCallService.LiveAntiKey + LiveRoomCallService.BizId + "_" + id + Long.toHexString(ltime)).getBytes()) + "&txTime=" + time);
		reclivenotifyBean.setAppid(AppId);
		reclivenotifyBean.setDuration("120");
		reclivenotifyBean.setEnd_time(String.valueOf(YMTDateUtil.getBeforeOrNextDate(-1).getTime()).substring(0, 10));
		reclivenotifyBean.setFile_format("hls");
		reclivenotifyBean.setFile_id(TestDataManager.getRandomUUID().toString().replaceAll("-", ""));
		reclivenotifyBean.setFile_size("20000");
		reclivenotifyBean.setStart_time(String.valueOf(YMTDateUtil.getBeforeOrNextDate(0).getTime()).substring(0, 10));
		String video_id = AppId + "_" + TestDataManager.getRandomNum(27);
		reclivenotifyBean.setVideo_url("http://" + AppId + ".vod2.myqcloud.com/" + video_id + "/playlist.m3u8");
		reclivenotifyBean.setVideo_id(video_id);
		return reclivenotifyBean;
	}

	/**
	 * 添加脏字管理
	 * 
	 * @param dirtyword
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void AddDirtyWords(String dirtyword) throws ClientProtocolException, IOException {
		Logger.comment("添加脏字列表");
		List<String> dirtyWords = Arrays.asList(dirtyword);
		AddDirtyWordsBean adddirtywordsBean = new AddDirtyWordsBean();
		AddDirtyWordsCall adddirtywordsCall = new AddDirtyWordsCall();
		adddirtywordsBean.setDirtyWords(dirtyWords);
		adddirtywordsCall.setData(false, adddirtywordsBean);
		adddirtywordsCall.callService();
	}

	/**
	 * 强制系统关闭 会修改直播时间和状态
	 * 
	 * @param liveid
	 * @throws IOException
	 * @throws ClientProtocolException
	 * @throws InterruptedException 
	 */
	public static void SysCloseLive(Integer liveid) throws ClientProtocolException, IOException, InterruptedException {
		LiveRoomWapper liveRoomWapper = new LiveRoomWapper();
		liveRoomWapper.updateLiveRoomTime(YMTDateUtil.getBeforeOrNextMunite(-100), YMTDateUtil.getBeforeOrNextMunite(-50), liveid);
		if (liveRoomWapper.selectLiveRoomById(liveid).getLiveStatus() == 2) {
			liveRoomWapper.updateLiveRoomPushStatus(liveid, 3);
		}
		SysCloseLiveCall syscloseliveCall = new SysCloseLiveCall();
		SysCloseLiveBean syscloseliveBean = new SysCloseLiveBean();
		syscloseliveCall.setData(syscloseliveBean);
		syscloseliveCall.callService();
		Thread.sleep(3000);
	}

	/**
	 * 查询脏字列表
	 * 
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static JsonArray QueryDirtyWords() throws ClientProtocolException, IOException {
		Logger.comment("查询脏字列表");
		QueryDirtyWordsBean querydirtywordsBean = new QueryDirtyWordsBean();
		QueryDirtyWordsCall querydirtywordsCall = new QueryDirtyWordsCall();
		querydirtywordsBean.setAppId("autoTest");
		querydirtywordsCall.setData(false, querydirtywordsBean);
		querydirtywordsCall.callService();
		return querydirtywordsCall.getJsonObject().get("data").getAsJsonArray();
	}

	/**
	 * 第三方通知
	 * 
	 * @param streamlivenotifyBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void LiveNotify(LiveNotifyBean streamlivenotifyBean) throws ClientProtocolException, IOException {
		Logger.comment("第三方通知:Event_type:" + streamlivenotifyBean.getEvent_type());
		LiveNotifyCall livenotifyCall = new LiveNotifyCall();
		livenotifyCall.setData(false, streamlivenotifyBean);
		livenotifyCall.callService();
	}

	/**
	 * 创建聊天室groupid
	 * 
	 * @param groupname
	 * @return groupid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String CreateGroup(String groupname) throws ClientProtocolException, IOException {
		Logger.comment("创建群:groupname:" + groupname);
		CreateGroupBean creategroupBean = new CreateGroupBean();
		CreateGroupCall creategroupCall = new CreateGroupCall();
		creategroupBean.setGroupName(groupname);
		creategroupCall.setData(false, creategroupBean);
		creategroupCall.callService();
		if(!creategroupCall.getString("code").equals("200")){
			creategroupCall.callService();
		}
		return creategroupCall.getString("data");
	}

	/**
	 * 创建直播bean 初始化默认Createsellerlive参数
	 * 
	 * @return
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static CreateSellerLiveBean initCreatesellerliveBean(Userinfo uinfo) throws InterruptedException, ClientProtocolException, IOException {
		if (uinfo == null) {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			uinfo = list.get(new Random().nextInt(list.size() - 1));
		}
		CreateSellerLiveBean createsellerliveBean = new CreateSellerLiveBean();
		LiveRoom livedto = new LiveRoom();
		livedto.setCountryId(1);
		livedto.setEndTime(YMTDateUtil.getBeforeOrNextMunite(120));
		// 第三方创建较慢 使用模拟数据
		// livedto.setGroupId(CreateGroup("AutoTestGroup" +
		// TestDataManager.getRandomNum(1)));
		livedto.setGroupId(String.valueOf(YMTDateUtil.getDate().getTime()));
		livedto.setStartTime(YMTDateUtil.getBeforeOrNextMunite(30));
		livedto.setLatitude("31." + TestDataManager.getRandomNum(4));
		livedto.setLongitude("100." + TestDataManager.getRandomNum(4));
		livedto.setLiveAddress("live地址" + TestDataManager.getRandomNum(4));
		livedto.setLiveCover("http://www.baidu.com/img/baidu_jgylogo3.gif");
		livedto.setLiveTitle("直播标题" + TestDataManager.getRandomNum(18));
		livedto.setShoppingAddress("ShoppingAddress:" + TestDataManager.getRandomNum(4));
		livedto.setPreVideoHeight(100);
		livedto.setPreVideoLength(1500);
		livedto.setPreVideoPic("http://www.baidu.com/img/baidu_jgylogo2.gif");
		livedto.setPreVideoWidth(200);
		livedto.setPreVideoUrl("http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4");
		livedto.setSellerName(uinfo.getLoginId());
		livedto.setUserId(uinfo.getUserId());
		createsellerliveBean.setLiveDto(livedto);
		createsellerliveBean.setRequestId(TestDataManager.getRandomUUID().toString());
		return createsellerliveBean;
	}

	/**
	 * 强制更新数据库时间为当前时间之前5分钟(开始)
	 * 
	 * @param liveid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void UpdateLiveTimeToStart(Integer liveid) throws ClientProtocolException, IOException {
		String time = YMTDateUtil.getBeforeOrNextMunite(-10);
		Logger.comment("更新开始时间:" + time);

		new LiveRoomWapper().updateLiveRoomTime(time, null, liveid);
	}

	/**
	 * 排序直播
	 * 
	 * @param liveid
	 * @param uid
	 * @param ProductIdList
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SortLiveProduct(Integer liveid, Integer uid, List<String> ProductIdList) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":主排序直播商品:uid:" + uid);
		SortLiveProductBean sortliveproductBean = new SortLiveProductBean();
		SortLiveProductCall sortliveproductCall = new SortLiveProductCall();
		sortliveproductBean.setUserId(uid);
		sortliveproductBean.setLiveId(liveid);
		sortliveproductBean.setProductIdList(ProductIdList);
		sortliveproductCall.setData(sortliveproductBean);
		sortliveproductCall.callService();
	}

	/**
	 * 强制主播上线,会修改开始时间
	 * 
	 * @param liveid
	 * @param uid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SellerLiveOnline(Integer liveid, Integer uid) throws ClientProtocolException, IOException {
		UpdateLiveTimeToStart(liveid);
		Logger.comment(liveid + ":主播上线:uid:" + uid);
		SellerLiveOnlineBean sellerliveonlineBean = new SellerLiveOnlineBean();
		SellerLiveOnlineCall sellerliveonlineCall = new SellerLiveOnlineCall();
		sellerliveonlineBean.setLiveId(liveid);
		sellerliveonlineBean.setUserId(uid);
		sellerliveonlineCall.setData(sellerliveonlineBean);
		sellerliveonlineCall.callService();
	}
	public static void SellerLiveOnline(Integer liveid, Integer uid,boolean ut) throws ClientProtocolException, IOException {
		if(ut)UpdateLiveTimeToStart(liveid);
		Logger.comment(liveid + ":主播上线:uid:" + uid);
		SellerLiveOnlineBean sellerliveonlineBean = new SellerLiveOnlineBean();
		SellerLiveOnlineCall sellerliveonlineCall = new SellerLiveOnlineCall();
		sellerliveonlineBean.setLiveId(liveid);
		sellerliveonlineBean.setUserId(uid);
		sellerliveonlineCall.setData(sellerliveonlineBean);
		sellerliveonlineCall.callService();
	}
	public static void RemoveSellerLive(Integer liveid, Integer uid) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":删除直播:uid:" + uid);
		RemoveSellerLiveBean removesellerliveBean = new RemoveSellerLiveBean();
		RemoveSellerLiveCall removesellerliveCall = new RemoveSellerLiveCall();
		removesellerliveBean.setLiveId(liveid);
		removesellerliveBean.setUserId(uid);
		removesellerliveCall.setData(removesellerliveBean);
		removesellerliveCall.callService();
	}

	/**
	 * 创建直播
	 * 
	 * @param createsellerliveBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException 
	 */
	public static LiveRoom CreateSellerLive(CreateSellerLiveBean createsellerliveBean) throws ClientProtocolException, IOException, InterruptedException {
		return CreateSellerLive(createsellerliveBean, true);
	}
	/**
	 * 订阅直播
	 * @param liveid
	 * @param userid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void SubscribeLive(Integer liveid,Integer userid) throws ClientProtocolException, IOException, InterruptedException {
		SubscribeBean subscribeBean=new SubscribeBean();
		SubscribeCall subscribeCall=new SubscribeCall();
		subscribeBean.setLiveId(liveid);
		subscribeBean.setUserId(userid);
		subscribeCall.setData(subscribeBean);
		subscribeCall.callService();			
	}
	
	/**
	 * 取消订阅直播
	 * @param liveid
	 * @param userid
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void UnSubscribeLive(Integer liveid,Integer userid) throws ClientProtocolException, IOException, InterruptedException {
		UnSubscribeBean unsubscribeBean=new UnSubscribeBean();
		UnSubscribeCall unsubscribeCall=new UnSubscribeCall();
		unsubscribeBean.setLiveId(liveid);
		unsubscribeBean.setUserId(userid);
		unsubscribeCall.setData(unsubscribeBean);
		unsubscribeCall.callService();		
	}
	/**
	 * 添加商品到直播
	 * 
	 * @param liveid
	 *            liveid
	 * @param userid
	 *            sellerid
	 * @param pid
	 *            productid
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public static void AddProductsToLive(Integer liveid, Integer userid, String pid) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":添加商品:pid:" + pid);
		AddProductsToLiveBean addproductstoliveBean = new AddProductsToLiveBean();
		AddProductsToLiveCall addproductstoliveCall = new AddProductsToLiveCall();
		addproductstoliveBean.setLiveId(liveid);
		List<String> ProductIdList = new ArrayList<>();
		ProductIdList.add(pid);
		addproductstoliveBean.setProductIdList(ProductIdList);
		addproductstoliveBean.setUserId(userid);
		addproductstoliveCall.setData(false, addproductstoliveBean);
		addproductstoliveCall.callService();
	}

	/**
	 * 重载 批量添加
	 * 
	 * @param liveid
	 * @param userid
	 * @param ProductIdList
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void AddProductsToLive(Integer liveid, Integer userid, List<String> ProductIdList) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":添加商品:ProductIdList:" + ProductIdList);
		AddProductsToLiveBean addproductstoliveBean = new AddProductsToLiveBean();
		AddProductsToLiveCall addproductstoliveCall = new AddProductsToLiveCall();
		addproductstoliveBean.setLiveId(liveid);
		addproductstoliveBean.setProductIdList(ProductIdList);
		addproductstoliveBean.setUserId(userid);
		addproductstoliveCall.setData(false, addproductstoliveBean);
		addproductstoliveCall.callService();
	}

	/**
	 * 设置置顶商品
	 * 
	 * @param liveid
	 * @param pid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SetLiveProductTop(Integer liveid, String pid) throws ClientProtocolException, IOException {
		SetLiveProductTopBean setliveproducttopBean = new SetLiveProductTopBean();
		SetLiveProductTopCall setliveproducttopCall = new SetLiveProductTopCall();
		setliveproducttopBean.setLiveId(liveid);
		setliveproducttopBean.setProductId(pid);
		setliveproducttopCall.setData(setliveproducttopBean);
		setliveproducttopCall.callService();
	}

	/**
	 * 关闭直播 慎用
	 * 
	 * @param liveid
	 * @param userid
	 * @param type
	 *            7 - 系统自动关闭直播，8 - 关闭直播，9 - 结束直播，10 - 强制关停<br>
	 *            7需要结束时间超过当前时间且断流<br>
	 *            8需要没有推流过<br>
	 *            9需要推流过而且当前时间超过开始时间<br>
	 *            10<br>
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void CloseSellerLive(Integer liveid, Integer userid, Integer type) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":关闭直播:type:" + type);
		if (type.equals(8)) {
			CloseSellerLiveBean closesellerliveBean = new CloseSellerLiveBean();
			CloseSellerLiveCall closesellerliveCall = new CloseSellerLiveCall();
			closesellerliveBean.setLiveId(liveid);
			closesellerliveBean.setUserId(userid);
			closesellerliveBean.setCloseType("CloseLive");
			closesellerliveCall.setData(false, closesellerliveBean);
			closesellerliveCall.callService();
		} else if (type.equals(9)) {
			CloseSellerLiveBean closesellerliveBean = new CloseSellerLiveBean();
			CloseSellerLiveCall closesellerliveCall = new CloseSellerLiveCall();
			closesellerliveBean.setLiveId(liveid);
			closesellerliveBean.setUserId(userid);
			closesellerliveBean.setCloseType("FinishLive");
			closesellerliveCall.setData(false, closesellerliveBean);
			closesellerliveCall.callService();
		}
	}

	/**
	 * 自动关闭用户的全部直播
	 * 
	 * @param uid
	 * @throws IOException
	 * @throws ClientProtocolException
	 * @throws InterruptedException 
	 */
	public static void AutoCloseLiveRoomByUserId(Integer uid) throws ClientProtocolException, IOException, InterruptedException {
		List<LiveRoom> roomlist = new LiveRoomWapper().selectRunningLiveRoomByUserid(uid);
		
		for (LiveRoom liveRoom : roomlist) {
			LiveRoomWapper liveRoomWapper = new LiveRoomWapper();
			liveRoomWapper.updateLiveRoomTime(YMTDateUtil.getBeforeOrNextMunite(-100), YMTDateUtil.getBeforeOrNextMunite(-50), liveRoom.getId());
			CloseSellerLiveBean closesellerliveBean = new CloseSellerLiveBean();
			CloseSellerLiveCall closesellerliveCall = new CloseSellerLiveCall();
			closesellerliveBean.setLiveId(liveRoom.getId());
			closesellerliveBean.setUserId(uid);
			if (liveRoom.getLivePush() == 0) {
				Logger.comment(liveRoom.getId() + ":已有直播自动关闭");
				closesellerliveBean.setCloseType("CloseLive");
			} else if (liveRoom.getLivePush() == 1) {
				Logger.comment(liveRoom.getId() + "已有直播自动结束");
				closesellerliveBean.setCloseType("FinishLive");
			}
			closesellerliveCall.setData(false, closesellerliveBean);
			closesellerliveCall.callService();
			if(!closesellerliveCall.getString("Code").equals("200")){
				SysCloseLive(liveRoom.getId());
			}
		}
	}

	/**
	 * 创建直播
	 * 
	 * @param createsellerliveBean
	 * @param autoclose
	 *            是否自动关闭已存在直播
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException 
	 */
	public static LiveRoom CreateSellerLive(CreateSellerLiveBean createsellerliveBean, boolean autoclose) throws ClientProtocolException, IOException, InterruptedException {
		Logger.comment("创建直播:autoclose:" + autoclose);
		if (autoclose) {
			AutoCloseLiveRoomByUserId(createsellerliveBean.getLiveDto().getUserId());
		}
		CreateSellerLiveCall createsellerliveCall = new CreateSellerLiveCall();
		createsellerliveCall.setData(false, createsellerliveBean);
		createsellerliveCall.callService();
		int liveid = createsellerliveCall.getJsonObject().get("Data").getAsJsonObject().get("LiveId").getAsInt();
		LiveRoom dblive = new LiveRoomWapper().selectLiveRoomById(liveid);
		dblive.setStartTime(dblive.getStartTime().substring(0, 19));
		dblive.setEndTime(dblive.getEndTime().substring(0, 19));
		return dblive;
	}

	/**
	 * 创建live 基本调用
	 * 
	 * @param createsellerliveBean
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static CreateSellerLiveCall CreateLive(CreateSellerLiveBean createsellerliveBean) throws ClientProtocolException, IOException {
		CreateSellerLiveCall createsellerliveCall = new CreateSellerLiveCall();
		createsellerliveCall.setData(false, createsellerliveBean);
		createsellerliveCall.callService();
		return createsellerliveCall;
	}

	/**
	 * 加入群
	 * 
	 * @param liveid
	 * @param userId
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void enterGroup(int liveid, int userId) throws InterruptedException, ClientProtocolException, IOException {
		Logger.comment(liveid + ":加入群:uid:" + userId);
		EnterGroupBean enterGroupBean = new EnterGroupBean();
		EnterGroupCall enterGroupCall = new EnterGroupCall();
		enterGroupBean.setLiveId(liveid);
		enterGroupBean.setUserId(userId);
		enterGroupCall.setData(false, enterGroupBean);
		enterGroupCall.callService();
	}

	/**
	 * 退出群
	 * 
	 * @param liveid
	 * @param userId
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void quitGroup(int liveid, int userId) throws InterruptedException, ClientProtocolException, IOException {
		Logger.comment(liveid + ":退出群:uid:" + userId);
		QuitGroupBean quitGroupBean = new QuitGroupBean();
		QuitGroupCall quitGroupCall = new QuitGroupCall();
		quitGroupBean.setLiveId(liveid);
		quitGroupBean.setUserId(userId);
		quitGroupCall.setData(false, quitGroupBean);
		quitGroupCall.callService();
	}

	/**
	 * 增加/获取比心数
	 * 
	 * @param liveid
	 * @param userId
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static int getLikeCount(int liveid, int userId, int count) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":比心:count:" + count);
		LikeBean likeBean = new LikeBean();
		LikeCall likeCall = new LikeCall();
		likeBean.setLiveId(liveid);
		likeBean.setUserId(userId);
		likeBean.setCount(count);
		likeCall.setData(false, likeBean);
		likeCall.callService();
		return likeCall.getJsonObject().get("data").getAsJsonObject().get("totalCount").getAsInt();
	}

	/**
	 * 暂停销售
	 * 
	 * @param liveid
	 * @param pid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void SuspendLiveProduct(int liveid, String pid) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":暂停销售:pid:" + pid);
		SuspendLiveProductBean suspendliveproductBean = new SuspendLiveProductBean();
		SuspendLiveProductCall suspendliveproductCall = new SuspendLiveProductCall();
		suspendliveproductBean.setLiveId(liveid);
		suspendliveproductBean.setProductId(pid);
		suspendliveproductCall.setData(false, suspendliveproductBean);
		suspendliveproductCall.callService();
	}

	/**
	 * 恢复销售
	 * 
	 * @param liveid
	 * @param pid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void ResumeLiveProduct(int liveid, String pid) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":恢复销售:pid:" + pid);
		ResumeLiveProductBean resumeliveproductBean = new ResumeLiveProductBean();
		ResumeLiveProductCall resumeliveproductCall = new ResumeLiveProductCall();
		resumeliveproductBean.setLiveId(liveid);
		List<String> ProductIdList = new ArrayList<>();
		ProductIdList.add(pid);
		resumeliveproductBean.setProductIds(ProductIdList);
		resumeliveproductCall.setData(resumeliveproductBean);
		resumeliveproductCall.callService();
	}

	/**
	 * @param liveid
	 * @param pid
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void RemoveLiveProduct(int liveid, String pid) throws ClientProtocolException, IOException {
		Logger.comment(liveid + ":移除商品:pid:" + pid);
		RemoveLiveProductBean removeliveproductBean = new RemoveLiveProductBean();
		RemoveLiveProductCall removeliveproductCall = new RemoveLiveProductCall();
		removeliveproductBean.setLiveId(liveid);
		removeliveproductBean.setProductId(pid);
		removeliveproductCall.setData(false, removeliveproductBean);
		removeliveproductCall.callService();
	}

	/**
	 * 产线用数据
	 * 
	 * @param userid
	 * @param loginid
	 * @return
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static CreateSellerLiveBean initOnlineCreatesellerliveBean(Integer userid, String loginid) throws InterruptedException, ClientProtocolException, IOException {
		CreateSellerLiveBean createsellerliveBean = new CreateSellerLiveBean();
		LiveRoom livedto = new LiveRoom();
		livedto.setCountryId(1);
		livedto.setEndTime(YMTDateUtil.getBeforeOrNextMunite(60 * 4));
		// 第三方创建较慢 使用模拟数据
		// livedto.setGroupId(CreateGroup("AutoTestGroup" +
		// TestDataManager.getRandomNum(1)));
		livedto.setGroupId(CreateGroup("AutoTestGroup"));
		livedto.setStartTime(YMTDateUtil.getBeforeOrNextMunite(62));
		livedto.setLatitude("31." + TestDataManager.getRandomNum(4));
		livedto.setLongitude("100." + TestDataManager.getRandomNum(4));
		livedto.setLiveAddress("live地址");
		livedto.setLiveCover("http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg");
		livedto.setLiveTitle("AT测试直播-今天的主题是?");
		livedto.setShoppingAddress("上海");
		livedto.setPreVideoHeight(100);
		livedto.setPreVideoLength(1500);
		livedto.setPreVideoPic("http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg");
		livedto.setPreVideoWidth(200);
		livedto.setPreVideoUrl("http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4");
		livedto.setSellerName(loginid);
		livedto.setUserId(userid);
		createsellerliveBean.setLiveDto(livedto);
		createsellerliveBean.setRequestId(TestDataManager.getRandomUUID().toString());
		return createsellerliveBean;
	}

	/**
	 * @param liveid
	 * @param userid
	 * @param tabindex
	 * @return
	 * @throws InterruptedException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static LiveProductListCall GetLiveProductList(Integer liveid, Integer userid, int tabindex) throws InterruptedException, ClientProtocolException, IOException {
		LiveProductListBean liveproductlistBean = new LiveProductListBean();
		LiveProductListCall liveproductlistCall = new LiveProductListCall();
		liveproductlistBean.setLiveId(liveid);
		liveproductlistBean.setUserId(userid);
		liveproductlistBean.setTabIndex(tabindex);
		liveproductlistCall.setData(liveproductlistBean);
		liveproductlistCall.callService();
		return liveproductlistCall;
	}

	public static void main(String[] args) throws Exception {
		//产线数据准备
		//商品数据 来自app的sqlserver integratedproduct库
		//select sProductId,iUserId from ymt_products where sproduct = 'ceshiwupai' and ibrandid = 11634 and daddtime<GetDate() and daddtime >2017/10/24 and iuserid=21158951
		
		Integer userid = 21158951;
		String uname = "sstest2";
		CreateSellerLiveBean initcreatebean = LiveRoomCallService.initOnlineCreatesellerliveBean(userid, uname);
		CreateSellerLiveCall createsellerliveCall = LiveRoomCallService.CreateLive(initcreatebean);
		int liveid = createsellerliveCall.getJsonObject().get("Data").getAsJsonObject().get("LiveId").getAsInt();
		List<String> ProductIdList = new ArrayList<>();
		ArrayList<String> list = TestDataManager.getLines("data");
		int count=0;
		for (String line : list) {
			String[] ss = line.split("	");
			if(ss[1].equals(userid.toString())&&count<20){
				ProductIdList.add(ss[0]);
				count++;
			}
		}
		LiveRoomCallService.AddProductsToLive(liveid, userid, ProductIdList);
		Thread.sleep(60000*2);
		LiveRoomCallService.SellerLiveOnline(liveid, userid,false);
		LiveRoomCallService.SetLiveProductTop(liveid, ProductIdList.get(0));
	}

	public static ReplayNotifyBean initReplayNotifyBean(Integer id, String file_id) {
		ReplayNotifyBean rb=new ReplayNotifyBean();
		
		MetaData metaData=new MetaData();
		Drm drm=new Drm();
		List<Trancode> processTaskList=new ArrayList<Trancode>();
		ProcedureStateChanged pc=new ProcedureStateChanged();
		rb.setEventType("ProcedureStateChanged");//需要校验
		rb.setVersion("4.0");
		rb.setData(pc);
		pc.setErrCode(0);//需要校验
		pc.setFileId(file_id);//需要校验
		pc.setMessage("");
		pc.setMetaData(metaData);
		pc.setStatus("FINISH");
		pc.setDrm(drm);
		pc.setProcessTaskList(processTaskList);
		Trancode e=new Trancode();
		e.setTaskType("Transcode");//需要校验
		e.setErrCode(0);//需要校验
		e.setStatus("SUCCESS");//需要校验
		TrancodeInPut input=new TrancodeInPut();
		input.setDefinition(220);//需要校验
		e.setInput(input);
		TrancodeOutPut output=new TrancodeOutPut();
		output.setUrl("http://"+AppId+".vod2.myqcloud.com/"+id+"/f20.hls");//需要校验
		output.setContainer("hls");//需要校验
		e.setOutput(output);
		processTaskList.add(e);
		return rb;
	}
}
